<template>
    <div>
        <el-table :data="store.getters['specs/getSpecsList']" border style="width: 100%" ref="menuRef">
            <el-table-column prop="id" label="规格编号" width="180" />
            <el-table-column prop="specsname" label="规格名称" width="180" />
            <el-table-column label="规格属性">
                <template v-slot="item">
                    <div>
                        <p v-for="(attr, index) in item.row.attrs" :key="index"> {{ attr }}</p>
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="status" label="状态">
                <template v-slot="item">
                    <div>
                        {{ item.row.status == 1 ? '启用' : '禁用' }}
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template v-slot="item">
                    <div>
                        <el-button type="primary" @click="edit(item.row.id)">编辑</el-button>
                        <el-button type="danger" @click="del(item.row.id)">删除</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination background layout="prev, pager, next" :page-size="store.getters['specs/getSize']" :total="store.getters['specs/getCount']" @current-change="changePage" />
    </div>
</template>
<script lang='ts' setup>
import { useStore } from 'vuex';
import { defineEmits } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { specsdelete } from '@/request/api';

// 调用仓库方法
let store = useStore();
// 调用仓库中数据
// 获取列表
store.dispatch('specs/getSpecsListActions');
// 获取总条数
store.dispatch('specs/getCountActions');
// 获取

// 调用子传父方法
let emit = defineEmits(['edit']);
// 编辑
const edit = (id: number) => {
    emit('edit', id);
}

// 删除
const del = (id: number) => {
    ElMessageBox.confirm(
        '确定要删除此数据吗?',
        '警告',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            specsdelete({ id }).then((res: any) => {
                if (res.code == 200) {
                    // 删除成功
                    ElMessage.success(res.msg)
                    // 重新渲染视图
                    store.dispatch('specs/getSpecsListActions');
                    // 重新获取总条数
                    store.dispatch('specs/getCountActions');
                } else {
                    // 删除失败
                    ElMessage.error(res.msg)

                }
            })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '已取消删除',
            })
        })

}

// 切换页码
const changePage = (num:Number)=>{
console.log(num);
store.dispatch('specs/changePageActions',num)
}
</script>
<style scoped lang='less'>
.el-pagination {
    float: right;
    margin-top: 20px;
}
</style>